/**
 * @Author: LiMeng
 * @Date: 2017/4/5
 */

var add_contractorInfo_func = function () {
    parent.layer.open({
        type: 2,
        title: '添加承包商',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/contractor/contractorInfo/add.shtml?resourceId=' + $.getQueryParam('resourceId')
    });
};
var edit_contractorInfo_func = function () {
    var $selected = $('#dg1').datagrid('getSelections');
    if ($selected.length !== 1) {
        layer.alert('请选择一个承包商！');
        return false;
    }

    parent.layer.open({
        type: 2,
        title: '编辑承包商',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/contractor/contractorInfo/edit.shtml?resourceId=' + $.getQueryParam('resourceId') + '&id=' + $selected[0].id
    });
};
var delete_contractorInfo_func = function () {
    var $selected = $('#dg1').datagrid('getSelections');
    if ($selected.length !== 1) {
        layer.alert('请选择一个承包商！');
        return false;
    }

    var index = parent.layer.confirm('是否确定删除？', function () {
        $.ajax({
            type: "POST",
            url: ROOT_PATH + '/contractor/contractorInfo/delete.shtml',
            data: {id: $selected[0].id},
            success: function (result) {
                result = $.parseJSON(result);
                if (result.success) {
                    parent.layer.msg(result.message, {time: 1000, icon: 1}, function () {
                        $('#dg1').datagrid('load');
                        parent.layer.close(index); //先得到当前iframe层的索引
                    });
                } else {
                    parent.layer.msg(result.message);
                }
            }
        });
    });
};
var getSelectedUser = function (data) {
    data = data[0];

    $('#addUserName').textbox('setValue', data.realName);
    $('#addUserId').val(data.id);
};
var openSelectUser = function (title, callback) {
    WindowManager.openLayer({
        type: 2,
        title: title,
        area: ['900px', '500px'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml',
        callBack: callback
    });
};
$(function () {
    $('#addUserName').textbox({
        icons: [{
            iconCls: 'icon-search',
            handler: function () {
                openSelectUser('选择登记人', getSelectedUser);
            }
        }],
        editable: false
    });
    $('#addDeptId').combotree({
        url: ROOT_PATH + '/permission/department/getDepartmentTree.shtml?parentId=0',
        editable: false
    });
    //初始化加载datagrid
    $('#dg1').datagrid({
        method: 'post',
        url: ROOT_PATH + '/contractor/contractorInfo/list.shtml',
        queryParams: {
            sort: 'addTime',
            order: 'desc'
        },
        toolbar: '#toolbar',
        fitColumns: true,
        pagination: true,
        rownumbers: true,
        sortOrder: true,
        striped: true,
        fit: true,
        columns: [[
            {field: 'ckid', checkbox: true},
            {field: 'code', title: '承包商代码', width: 80, sortable: true},
            {field: 'name', title: '承包商名称', width: 100, sortable: true},
            {field: 'fullName', title: '承包商全称', width: 100, sortable: true},
            {field: 'faxId', title: '税码/社会信用代码', width: 100},
            {field: 'postCode', title: '邮政编码', width: 80},
            {field: 'address', title: '地址', width: 100},
            {field: 'countryId', title: '国家ID', width: 50, hidden: true},
            {field: 'countryName', title: '国家', width: 50},
            {field: 'provinceId', title: '省市ID', width: 50, hidden: true},
            {field: 'provinceName', title: '省市', width: 50},
            {field: 'cityId', title: '城市ID', width: 50, hidden: true},
            {field: 'cityName', title: '城市', width: 50},
            {field: 'countyId', title: '区县ID', width: 60, hidden: true},
            {field: 'countyName', title: '区县', width: 60},
            {
                field: 'useState', title: '状态', width: 50, sortable: true, formatter: function (value, row, index) {
                if (value === 1) {
                    return '已启用';
                } else {
                    return '未启用';
                }
            }
            },
            {field: 'addUserId', title: '登记人id', width: 100, sortable: true, hidden: true},
            {
                field: 'addUserName', title: '登记人', width: 100, formatter: function (value, row, index) {
                if (row.addUser) {
                    return row.addUser.realName;
                } else {
                    return '';
                }
            }
            },
            {field: 'addDeptId', title: '登记部门id', width: 100, sortable: true, hidden: true},
            {
                field: 'addDeptName', title: '登记部门', width: 100, formatter: function (value, row, index) {
                if (row.addDept) {
                    return row.addDept.name;
                } else {
                    return '';
                }
            }
            },
            {field: 'addTime', title: '登记时间', width: 80, sortable: true},
            {field: 'remarks', title: '备注', width: 100}
        ]]
    });
    $('#btn_clear').on("click", function () {
        $('#tb1').form('reset');
        $('#tb2').form('reset').find(':hidden').val('');
    });
    $('#btn_search').on("click", function () {
        $('#dg1').datagrid('reload', $('#tb1,#tb2').find(':input').serializeJson());
    });
    $('#btn_advanced_search').on("click", function () {
        $('#tb2').toggle();
        $('#dg1').datagrid('resize');
    });
    $('#btn_contractorInfo_edit').on('click', function () {
        edit_contractorInfo_func();
    });
    $('#btn_contractorInfo_add').on('click', function () {
        add_contractorInfo_func();
    });
    $('#btn_contractorInfo_delete').on('click', function () {
        delete_contractorInfo_func();
    });
});